;(function($){
	$.open_table_oper = $.open_table_oper || {};
	$.fn.open_table_oper = function(opts){
		var el = this;
		var opt = {
			unOpenArr : [],
			ct_brzdArr : [],
			ktxfArr : [],
			ktjsArr : [],
			depositArr : [],
			rmpackage : [],
			dataBack : function(){},
			closeBack : function(){},
		}
		var param = $.extend(opt,opts);
		var app = new App(el,param);
		$.open_table_oper = $.extend($.open_table_oper,app);
	}
	
	var App = function(el,param){
		this.el = el;
		this.param = param;
		this.initHtml();
		var t = this;
		this.getShoupai(function(data){
			t.setSize();
			t.bindEvent();
		});
	}
	
	App.prototype = {
		bindEvent : function(){
			var el = this.el;
			var param = this.param;
			var t = this;
			//显示手牌
			el.find("#showCard").unbind("change").change(function(){
				if($(this).is(":checked")){
					el.find("#operBody_shoupai").removeClass("hide")   
				}else{
					el.find("#operBody_shoupai").addClass("hide")
				}
			});
			
			//开手牌
			el.find("#btnOpenShoupai").unbind("click").click(function(){
				
				var super_flag = $(".super_flag").closest(".each_undish").data("bh")||"";
				var arg = {"ct_group":{"bmh":param.bmh,jdh:$("#jdh").text(),pno:$("#pno").text(),
										market:$("#market").text(),remark:$("#remark").text()},
						   "arg":{"ticketfirstpay":"yes","super_flag":super_flag.toString()},
						   "ct_brzd":param.ct_brzdArr,"ktxf":param.ktxfArr,"ktjs":param.ktjsArr,"deposit":param.depositArr};
//				console.log(arg);
				t.openShoupai(arg);
			})
			
			//默认
			el.find("#btnDefault").unbind("click").click(function(){
				el.find("#operBody_item").find(".shoupaiDefault").trigger("click");
			});
			
			//刷新
			el.find("#btnRefresh").unbind("click").click(function(){
				t.refreshShoupai();
			});
			
			//选择公司
			el.find("#btnEditCompany").unbind("click").click(function(){
				$(this).modalFrame("destroy");
				$(this).modalFrame("init",function(modalEle){   
					$("#"+modalEle).modal("show"); 
					if(!$.isFunction($().selection_fromProfileCompany)){
						$.ajaxSetup({ async: false, cache: false });
			            $.getScript("js/openSpa/jquery.selection_fromProfileCompany.js");
			            $.ajaxSetup({ async: true });
					}
					$("#"+modalEle).selection_fromProfileCompany({
						condition:{ho_profile_where:{search:""}},//获取数据源条件json el.find("span[id=company]").text()
						dataBack : function(data){
							console.log(data);
							var js = {pno:data.pno,company:(data.gname||"")}
							param.selBack(js);
						},
						closeBack : function(){
							
						},
					});
				});
			})
			
			//接待信息
			el.find("#btnJdInfo").unbind("click").click(function(){
				$(this).modalFrame("destroy");
				$(this).modalFrame("init",function(modalEle){   
					$("#"+modalEle).modal("show"); 
					$("#"+modalEle).show();
					if(!$.isFunction($().spa_sel_jdInfo)){
						$.ajaxSetup({ async: false, cache: false });
			            $.getScript("js/openSpa/jquery.modal.spa_sel_jdInfo.js");
			            $.ajaxSetup({ async: true });
					}
					$("#"+modalEle).spa_sel_jdInfo({
						bmh : param.bmh,
						ds : $.open_table_list.param.jdInfo,
						dataBack : function(data){
							console.log(data);
							param.selBack(data);
						},
						closeBack : function(){
							
						}
					});
				});
			})
			
			//返回
			el.find("#btnBack").unbind("click").click(function(){
				history.go(-1);
			})
			
			//台号搜索
			el.find("#th").unbind("keyup").keyup(function(e){
				var value = $(this).val();
				el.find("#shouPaiContainer").find(".box[data-body-bh]").addClass("hide");
				el.find("#shouPaiContainer").find(".floorTitle").parent().show();
				if(value){
					el.find("#shouPaiContainer").find(".box[data-body-bh^="+value+"]").removeClass("hide");
				}else{
					el.find("#shouPaiContainer").find(".box[data-body-bh]").removeClass("hide");
				}
				
				//判断层是否显示
				el.find("#shouPaiContainer").find(".floorLine").each(function(k,v){
					var size = $(v).find(".box:visible").size();
					if(size==0){
						$(v).closest(".floorLine").parent().hide();
					}
				})
			})
		},
		initHtml : function(){
			var el = this.el;
			var param = this.param;
			var t = this;
			
			var html = 	'<div id="operBody_item">'+
						'</div>'+
						'<div id="operBody_shoupai" class="widget-box">'+
							'<div id="shouPaiContainer" class="panel-collapse" style="padding:0px;">'+//widget-main 
							
							'</div>'+
						'</div>'+
//						'<div id="operFooter" class="widget-box" style="height:63px;width: -webkit-fill-available;bottom: -63px;position: absolute;margin:0px;">'+
						'<div id="operFooter" class="widget-box" style="height:72px;width: -webkit-fill-available;bottom: -72px;position: absolute;margin:0px;">'+
							'<div class="pull-left">'+
								'<button type="button" id="btnDefault" class="btn btn-app btn-sm btn-grey" style=" height: 66px;">'+//57
									'<span>'+$.i18n.propJsp("默认")+'</span>'+
								'</button>'+
								'<button type="button" id="btnRefresh" class="btn btn-app btn-sm btn-grey" style=" height: 66px;">'+
									'<span>'+$.i18n.propJsp("刷新")+'</span>'+
								'</button>'+
								'<button type="button" id="btnEditCompany" class="btn btn-app btn-sm btn-grey hide" style=" height: 66px;">'+
									'<span>'+$.i18n.propJsp("公司")+'</span>'+
								'</button>'+
								'<button type="button" id="btnJdInfo" class="btn btn-app btn-sm btn-grey" style=" height: 66px;">'+
									'<span>'+$.i18n.propJsp("接待信息")+'</span>'+
								'</button>'+
							'</div>'+
							'<div id="" class="pull-right" style="">'+
								'<button type="button" id="btnOpenShoupai" class="btn btn-app btn-sm btn-purple" style=" height: 66px;">'+
									'<span>'+$.i18n.propJsp("开手牌")+'</span>'+
								'</button>'+
								'<button type="button" id="btnBack" class="btn btn-app btn-sm btn-grey" style=" height: 66px;">'+
									'<span>'+$.i18n.propJsp("返回")+'</span>'+
								'</button>'+
							'</div>'+
						'</div>';
			el.html(html);
		},
		getShoupai : function(back){
			var el = this.el;
			var param = this.param;
			var t = this;
			param.wait();
			$.HSAPI_CALLPROCEDURE2({},"SPAShoupaiOpenDefault",{},function(data){
				param.goon();
				if(data.RETURN==1){
					
					param.ct_th = data.ct_th;
					param.ct_brzd = data.ct_brzd;
					param.deposit = data.deposit;
					param.depositcode = data.depositcode;
					param.ktjs = data.ktjs;
					param.ktxf = data.ktxf;
					param.ds = data;
					param.dsBack(data);
					
					t.loadOperItem(data);
					//初始手牌
					t.initShoupaiHtml(data.ct_th);
				}else{
					if(data.hasOwnProperty("gritter")){
						if(data.hasOwnProperty("gritterstyle"))
							jf_successDelayed(data.gritter,2000,data.gritterstyle);
						else
							jf_successDelayed(data.gritter,2000); 
					}else{
						$.HSAPI_ALERT(data.MSG)
					}
				}
				back();
			},function(data){
				param.goon();
				$.HSAPI_ALERT(JSON.stringify(data));
				back();
			})
		},
		loadOperItem : function(data){
			var el = this.el;
			var param = this.param;
			var t = this;
			
			if(!$.isFunction($().get_item_html)){
				$.ajaxSetup({ async: false, cache: false });
	            $.getScript("js/openSpa/jquery.service.get_item_html.js");
	            $.ajaxSetup({ async: true });
			}
			el.find("#operBody_item").get_item_html({
				ct_th : data.ct_th,
				ct_brzd : data.ct_brzd,
				deposit : data.deposit,
				depositcode : data.depositcode,
				ktjs : data.ktjs,
				ktxf : data.ktxf,
				ds : data,
				dataBack : function(){},
				closeBack : function(){},
				setSizeBack : function(){
					t.setSize();
				},
			});
		},
		setSize : function(){
			var el = this.el;
			var param = this.param;
			var t = this;
			var top = el.find("#operBody_shoupai").offset().top;
			var winH = $(window).height();
			var offset = parseFloat($(".page-content").css("padding-top"))//页面偏差
			var h = winH - top - 41-7+46  - 78 //+ offset//68 ;
			el.find("#shouPaiContainer").css({"height":(h-20+offset),"overflow-y":"auto"});
			var shoupaiOper_h = el.find("#operFooter").offset().top - offset -107//-45 ;	
			// $("#shoupaiOper").css({"height":shoupaiOper_h});
		},
		openShoupai : function(arg){
			var el = this.el;
			var param = this.param;
			var t = this;
			
			$.HSAPI_CALLPROCEDURE2(arg,"SPAShouPaiOpen",{},function(data){
				if(data.RETURN==1){
					if(data.hasOwnProperty("gritter")){
						if(data.hasOwnProperty("gritterstyle"))
							jf_successDelayed(data.gritter,2000,data.gritterstyle);
						else
							jf_successDelayed(data.gritter,2000); 
					}else{
						jf_success(data.MSG)
					}
					t.refreshShoupai();
				}else if(data==-10){
					t.errorContent(arg,data)
				}else{
					if(data.hasOwnProperty("gritter")){
						if(data.hasOwnProperty("gritterstyle"))
							jf_successDelayed(data.gritter,2000,data.gritterstyle);
						else
							jf_successDelayed(data.gritter,2000); 
					}else{
						$.HSAPI_ALERT(data.MSG)
					}
				}
			},function(data){
				$.HSAPI_ALERT(JSON.stringify(data));
			})
		},
		errorContent:function(arg,data){
			var el = this.el;
			var param = this.param;
			var t = this;
			
			var MSG = data.MSG;
			var name = data.checkbox;
			var value = (data.checkboxvalue||"");
			
			var checked = "";
			if(value==1){
				checked = "checked"
			}
			
			var html = 	'<h3 class="header smaller lighter blue">'+
							'<i class="ace-icon fa fa-bullhorn"></i>'+MSG+
						'</h3>'+
						'<div class="alert alert-block alert-danger">'+
							'<label>'+
								'<input id="checkin_cccheckbox" type="checkbox" name="'+name+'" value="" class="ace" '+checked+'/>'+
								'<span class="lbl" style="color:#478fca;">'+MSG+'</span>'+
							'</label>'+
						'</div>';

			bootbox.dialog({
				message: html,
				buttons:{
					"button1":{
						"label" : $.i18n.propJsp("确定"),
						"className" : "btn-sm btn-info",
						"callback" : function(){
							if($("#checkin_cccheckbox").prop("checked")){
								arg.arg[name]="yes";
								t.openShoupai(arg);
							}else{
								arg.arg[name]="no";
								t.openShoupai(arg);
							}
						}
					},	
					"button" :
					{
						"label" : $.i18n.propJsp("取消"),
						"className" : "btn-sm",
					},
				}
			});
		},
		initShoupaiHtml : function(ds){
			var el = this.el;
			var param = this.param;
			var t = this;
			
			var floor="";
    		var itemDom=el.find("#shouPaiContainer"); 
    		itemDom.empty();
    		
    		var queryt = "select * from json order by floor";
    		ds = jsonsql.query(queryt,ds);
    		var html = ""
    		
    		var floor = 'startFor000';//用来区分是否是第一个循环
    		ds.map(x => {
    			
    			if(floor=='startFor000'){
    				floor = x.floor;
    				html = '<div style="display:flex !important;margin-bottom:3px;">'+
								'<div class="floorTitle" style="width:40px;display:flex;flex-direction:column;justify-content:center;align-items:center;font-size:16px;font-weight:bold;background:#b5d1e2;border-bottom: 1px solid #f5f5f5;">'+
								x.floor +
							'</div>'+
							'<div class="floorLine clearfix" id="floor-1" style="flex:1;">';
    			}else if(floor!=x.floor){
    				floor = x.floor;
    				html +=		'</div>'+
    						'</div>'+
    						'<div style="display:flex !important;margin-bottom:3px;">'+
	    						'<div class="floorTitle" style="width:40px;display:flex;flex-direction:column;justify-content:center;align-items:center;font-size:16px;font-weight:bold;background:#b5d1e2;border-bottom: 1px solid #f5f5f5;">'+
	    							floor +
	    						'</div>'+
	    						'<div class="floorLine clearfix" id="floor-1" style="flex:1;">';
    			}
    			
    			var	xfje=x.hasOwnProperty("xfje")?x.xfje:"";
    			var dfje=(x.hasOwnProperty("dfje")&&x.dfje>0.00)?"("+x.dfje+")":"";
    			
    			var handpanel_info={};
    			if(x.hasOwnProperty("handpanel_info")&&x.handpanel_info!="")
    				handpanel_info=JSON.parse(x.handpanel_info);
    			
    			var ico_info=[];
    			var ico_info_text="";
    			if(x.hasOwnProperty("ico_info")&&x.ico_info!="[]")
    				ico_info=JSON.parse(x.ico_info);
    			$.each(ico_info,function(key,value){ 
    				ico_info_text += getTextStyle(value); 
				})

				var height="60px";
				if(handpanel_info.hasOwnProperty("height"))
					height=handpanel_info.height;
				var width="100px";
				if(handpanel_info.hasOwnProperty("width")) 
					width=handpanel_info.width;
				var border="";
				if(handpanel_info.hasOwnProperty("border")&&handpanel_info.border>"0"&&handpanel_info.border!="")  
					border="border: "+handpanel_info.border+"px solid #cdd8e3;";
				var padding="padding: 0px;";
				if(handpanel_info.hasOwnProperty("padding")&&handpanel_info.padding!="")      
					padding="padding: "+handpanel_info.padding+"px;";
					
    			html +='<div class="box" data-body-floor="'+x.floor+'" data-bgcolor="'+handpanel_info.bgcolor+'" data-body-bh="'+x.bh+'" style="'+border+' background-color:'+handpanel_info.bgcolor+';width:'+width+' !important; height:'+height+' !important; '+padding+' float:left;margin-left:5px;margin-top:5px;">'+
						     '<span class="json hide">'+
						     	JSON.stringify(x)+
						     '</span>'+
    						'<div style="width: 100%;height:33%;float: left;"><span style="color:'+handpanel_info.tcolor+'">'+x.bh+'</span>'+ 
						      	'<small class="text-muted" style="color:'+handpanel_info.tcolor+' !important; font-size:60%;  float:right;">&nbsp;'+ico_info_text+ '</small>'+
						     '</div>'+
						     '<div style="width: 100%;height:33%;float: left;">'+ 
 					      	 	'<span style="float:right;color:'+handpanel_info.tcolor+'">'+xfje+'</span>'+  
 						     '</div>'+
						     '<div style="width: 100%;height:33%;float: left;white-space:nowrap; overflow:hidden; text-overflow:ellipsis;">'+  
						     	'<span style="float:right;color:'+handpanel_info.tcolor+'">'+dfje+'</span>'+  
				      	 	'</div>'+
					    '</div>';
    		});
    		
    		html += '</div></div>';
    		itemDom.html(html);
    		
    		//选择手牌 
			el.find("#shouPaiContainer").find(".box").unbind("click").click(function(){
				$(this).hide();
				var size = $(this).closest(".floorLine").find(".box:visible").size();
				if(size==0){
					$(this).closest(".floorLine").parent().hide();
				}
				var data = $.parseJSON($(this).find("span.json").text());
				data.bgcolor = $(this).data("bgcolor");
				var badgeJson = {"deposit":"","ktjs":"","ktxf":""};
				for(k in badgeJson){
					var name = el.find("#"+k+"Container").find(".btn.btn-yellow.active").data("name");
					var value = el.find("#"+k+"Container").find(".btn.btn-yellow.active").data("value");
					var text = el.find("#"+k+"Container").find(".btn.btn-yellow.active").data("text");
					if(!text){
						if(k=="ktxf"){
							if(data.xf_mc){
								text =  data.xf_mc + "&nbsp;" + data.xf_dj
								value = data.xf_bh;
							}
						}else if(k=="ktjs"){
							if(data.js_mc){
								text = data.js_mc
								value = data.js_bh;
							}
						}
					}
					badgeJson[k] = text;
					data[name] = value;
				}
				data.deposit = el.find("#deposit").val();
				if(data.deposit&&parseInt(data.deposit)!=0){
					badgeJson.deposit = el.find("#deposit").attr("data-type")+":"+el.find("#deposit").val();
				}
				data.xf_dj = el.find("#ktxfContainer").find("label.active").data("xf_dj")||(data.xf_dj||"");
				data.th = data.bh;
				data.dcode = el.find("#deposit").data("dcode");
				if(!data.cwlx_c){
					data.cwlx_c = "";
				}
				param.dataBack(data,badgeJson,$.parseJSON($(this).find("span.json").text()));
			})
    		
		},
		initShoupaiInfo : function(data){
			var el = this.el;
			var param = this.param;
			var t = this;
			
		},
		refreshShoupai : function(){
			var el = this.el;
			var param = this.param;
			var t = this;
			param.wait();
			param.pageFresh();
			$.HSAPI_CALLPROCEDURE2({},"SPAShoupaiOpenDefault",{},function(data){
				param.goon();
				if(data.RETURN==1){
					param.ct_th = data.ct_th;
					t.initShoupaiHtml(data.ct_th);
					param.selBack({});
				}else{
					if(data.hasOwnProperty("gritter")){
						if(data.hasOwnProperty("gritterstyle"))
							jf_successDelayed(data.gritter,2000,data.gritterstyle);
						else
							jf_successDelayed(data.gritter,2000); 
					}else{
						$.HSAPI_ALERT(data.MSG)
					}
				}
			},
			function(data){
				param.goon();
				$.HSAPI_ALERT(JSON.stringify(data));
			})
		},
		showShouPai : function(bh){
			var el = this.el;
			var param = this.param;
			var t = this;
			var size = el.find("#shouPaiContainer").find(".box[data-body-bh="+bh+"]").closest(".floorLine").find(".box:visible").size();
			if(size==0){
				el.find("#shouPaiContainer").find(".box[data-body-bh="+bh+"]").closest(".floorLine").parent().show();
			}
			el.find("#shouPaiContainer").find(".box[data-body-bh="+bh+"]").show();
		},
		changeSelTableOper : function(name,value,cla){
			var el = this.el;
			var param = this.param;
			var t = this;
			el.find("label."+cla+"[data-"+name+"="+value+"]").trigger("click");
		},
		hideShoupai : function(bh){
			var el = this.el;
			var param = this.param;
			var t = this;
			var size = el.find("#shouPaiContainer").find(".box[data-body-bh="+bh+"]").closest(".floorLine").find(".box:visible").size();
			if(size==1){
				el.find("#shouPaiContainer").find(".box[data-body-bh="+bh+"]").closest(".floorLine").parent().hide();
			}
			el.find("#shouPaiContainer").find(".box[data-body-bh="+bh+"]").hide();
		},
	}
	
	
	function getContextPath(){
		var pathName = document.location.pathname;
		var index = pathName.substr(1).indexOf("/");
		var path = pathName.substr(0, index + 1);
		return path;
	}

})(jQuery);